package com.motion.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.motion.dto.ArticleDto;
import com.motion.dto.UserArticlesDto;
import com.motion.generator.entity.Article;
import com.motion.generator.entity.Article;
import com.motion.generator.mapper.ArticleMapper;
import com.motion.generator.mapper.UserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class ArticleService {
    @Autowired(required = false)
    ArticleMapper articleMapper;
    @Autowired(required = false)
    UserMapper userMapper;
    @Autowired(required = false)
    UserArticleService userArticleService;
    public List<Article> articleList(Long page, Long size, int type, LocalDateTime starTime, LocalDateTime endTime, String input){
        QueryWrapper<Article> queryWrapper=new QueryWrapper<>();
        if (type!=2)
            queryWrapper.eq("ifRelease",type);
        if(starTime!=null)
            queryWrapper.ge("releaseTime",starTime);
        if (endTime!=null)
            queryWrapper.le("releaseTime",endTime);
        if (!input.equals(""))
            queryWrapper.like("content",input);
        queryWrapper.orderByDesc("releaseTime");
        IPage<Article> ArticlePage=articleMapper.selectPage(new Page<>(page,size),queryWrapper);
        return ArticlePage.getRecords();
    }
    public IPage<Article> articleIPage(Long page, Long size, int type){
        QueryWrapper<Article> queryWrapper=new QueryWrapper();
        queryWrapper.eq("type",type);
        return articleMapper.selectPage(new Page<>(page,size),queryWrapper);
    }


    public List<ArticleDto> Article2ArticleDto(List<Article> articleList){
        List<ArticleDto> ref=new ArrayList<>();
        ArticleDto articleDto =new ArticleDto();
        for (Article item:articleList){
            articleDto =new ArticleDto();
            BeanUtils.copyProperties(item, articleDto);
            articleDto.setUploadName(userMapper.selectById(item.getUploadId()).getNickName());
            ref.add(articleDto);
        }
        return ref;
    }
    public List<UserArticlesDto> Arcle2UserArticlesDto(List<Article> articleList,Long useId){
        List<UserArticlesDto> ref=new ArrayList<>();
        UserArticlesDto userArticlesDto=new UserArticlesDto();
        for (Article item:articleList){
            userArticlesDto=new UserArticlesDto();
            BeanUtils.copyProperties(item,userArticlesDto);
            userArticlesDto.setUploadName(userMapper.selectById(item.getUploadId()).getNickName());
            userArticlesDto.setCollected(userArticleService.userifCollect(useId,item.getArticleId()));
            ref.add(userArticlesDto);
        }
        return ref;
    }

    public Article insertArticle(Article article){
        articleMapper.insert(article);
        return article;
    }

    public int delectArticle(Long articleId){
        return articleMapper.deleteById(articleId);
    }
    public int updateArticle(Article article){
        return articleMapper.updateById(article);
    }
}
